using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Rilisoft.MiniJson;
using UnityEngine;

public class MarketSaleController : MonoBehaviour
{
	private static MarketSaleController _instance;

	private List<MarketSale> allSales = new List<MarketSale>();

	public bool jsonReaded;

	public static MarketSaleController instance
	{
		get
		{
			if (_instance == null)
			{
				GameObject gameObject = new GameObject();
				gameObject.name = "MarketSalesController(Don't Destroy)";
				gameObject.AddComponent<MarketSaleController>();
			}
			return _instance;
		}
	}

	public List<MarketSale> AllSales
	{
		get
		{
			return allSales;
		}
	}

	public bool EnableSalesInMarket
	{
		get
		{
			return AllSales.Any(delegate(MarketSale x)
			{
				if (!MarketController.Instance.allProducts.ContainsKey(x.productId))
				{
					return false;
				}
				Product product = MarketController.Instance.allProducts[x.productId];
				if (product.Purchased)
				{
					return false;
				}
				return (product.prevProduct == null) ? (product.tier <= CupsManager.CurrentTier) : (product.tier <= CupsManager.CurrentTier && product.prevProduct.Purchased);
			});
		}
	}

	public static event Action marketSalesJsonLoaded;

	public static event Action marketSalesParsedFromJson;

	private void Awake()
	{
		if (_instance != null)
		{
			UnityEngine.Object.DestroyImmediate(this);
			return;
		}
		_instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	public MarketSale GetSaleForProduct(string productId)
	{
		return AllSales.Find((MarketSale x) => x.productId == productId);
	}

	private void Start()
	{
		StartCoroutine(RequestAndLoadSalesJson());
	}

	private IEnumerator RequestAndLoadSalesJson()
	{
		yield return new WaitForSeconds(0.5f);
		string jsonText = JSONManager.Instance.GetJSON(URLs.MarketSales);
		string oldJson = string.Empty;
		while (jsonText == oldJson)
		{
			yield return new WaitForSeconds(3f);
			oldJson = jsonText;
			jsonText = JSONManager.Instance.GetJSON(URLs.MarketSales);
		}
		if (MarketSaleController.marketSalesJsonLoaded != null)
		{
			MarketSaleController.marketSalesJsonLoaded();
		}
		Dictionary<string, object> parsedJson = (Dictionary<string, object>)Json.Deserialize(jsonText);
		if (parsedJson != null)
		{
			if (parsedJson.ContainsKey("sales"))
			{
				List<object> marketSalesObject = parsedJson["sales"] as List<object>;
				ParseMarketSalesObject(marketSalesObject, allSales);
			}
			else
			{
				BCWDebug.Log("Market sales response doesn't contain “sales” property.", "Market sales");
			}
			jsonReaded = true;
		}
	}

	private void ParseMarketSalesObject(List<object> marketSalesObject, List<MarketSale> salesList)
	{
		if (marketSalesObject != null)
		{
			salesList.Clear();
			for (int i = 0; i < marketSalesObject.Count; i++)
			{
				Dictionary<string, object> dictionary = marketSalesObject[i] as Dictionary<string, object>;
				string productId = (string)dictionary["product_id"];
				int discount = Convert.ToInt32((!dictionary.ContainsKey("discount")) ? ((object)0) : dictionary["discount"]);
				int newPrice = Convert.ToInt32((!dictionary.ContainsKey("new_price")) ? ((object)0) : dictionary["new_price"]);
				int oldPrice = Convert.ToInt32((!dictionary.ContainsKey("old_price")) ? ((object)0) : dictionary["old_price"]);
				MoneyType newCurrency = ((!((string)dictionary["currency"] == "cash")) ? MoneyType.Gold : MoneyType.Cash);
				salesList.Add(new MarketSale(productId, newCurrency, oldPrice, newPrice, discount));
			}
			if (MarketSaleController.marketSalesParsedFromJson != null)
			{
				MarketSaleController.marketSalesParsedFromJson();
			}
		}
	}
}
